<?php

/**
 * This file is part of the Framework03 PHP framework.
 * @author Walter Tamboer
 * @package Framework03
 */

/**
 * The abstract class for all elements in a FW form.
 */
abstract class FW_Form_Element extends FW_Html_Element
{
    /**
     * @var string The label of this element.
     */
    protected $label = '';

    /**
     * @var bool Whether or not this form element is required.
     */
    protected $required = false;

    /**
     * @var array A list with all the validators of this element.
     */
    private $validatorList = array();

    /**
     * Initializes a new instance of this class.
     * @param string $name The name of this form element.
     * @param mixed $options The options of this element which can be stored in an array or config object.
     */
    public function __construct($name, $options = null)
    {
        $this->setAttribute('id', $name);
        $this->setAttribute('name', $name);
        $this->setAttribute('value', '');
    }

    /**
     * Checks if the element data is valid.
     * @return boolean Returns true when the data of this element is valid; false otherwise.
     */
    public function isValid($value)
    {
		$this->setValue($value);
		
        if ($value == '' && $this->required)
            return false;
            
        foreach ($this->validatorList as $validator)
        {
            if (!$validator->validate($value))
                return false;
        }
        
        return true;
    }

    /**
     * Adds a validator to this element.
     * @param FW_Validator_Abstract $validator
     * @return FW_Form_Element
     */
    public function addValidator(FW_Validator_Abstract $validator)
    {
        $this->validatorList[] = $validator;
        return $this;
    }

    /**
     * Sets a new name for this form element.
     * @param string $name The new name for this form element.
     */
    public function setName($name)
    {
        $this->setAttribute('name', $name);
    }

    /**
     * Gets the name of this form element.
     * @return string Returns a string with the name of this form element.
     */
    public function getName()
    {
        return $this->getAttribute('name');
    }
    
    /**
     * Sets a new value for this form element.
     * @param string $value The new value for this form element.
     */
    public function setValue($value)
    {
        $this->setAttribute('value', $value);
    }

    /**
     * Gets the value of this form element.
     * @return string Returns a string with the value of this form element.
     */
    public function getValue()
    {
        return $this->getAttribute('value');
    }
    
    /**
     * Sets a the value of the label for this element.
     * @param string $value The value for the label of this element.
     */
    public function setLabel($value)
    {
        $this->label = $value;
    }

    /**
     * Gets the value of the label of this element
     * @return string
     */
    public function getLabel()
    {
        return $this->label;
    }
    
    /**
     * Marks this element as required or not.
     * @param bool $value Whether or not this element is required.
     * @return FW_Form_Element
     */
    public function setRequired($value)
    {
        $this->required = $value;
        return $this;
    }

    /**
     * Gets a boolean value that indicates whether or not this element is required.
     * @return bool
     */
    public function isRequired()
    {
        return $this->required;
    }
}
